package main

import (
	"fmt"
)

/**
 * @description 
 * @time 2018/5/12 19:18
 * @version 
 */
func main() {
	channelDemo()
}

type worker struct {
	in   chan int
	done chan bool
}

func channelDemo() {
	var workers [10] worker
	for i := 0; i < 10; i++ {
		workers[i] = createWorker(i)
	}

	for i, worker := range workers {
		worker.in <- 'a' + i
		//收一个东西就读好了
		<- workers[i].done
		fmt.Println("111111111111111")
	}

	//for i, worker := range workers {
	//	worker.in <- 'A' + i
	//	//收done
	//	//<-workers[i].done
	//}
}

func doWorker(id int, c chan int, done chan bool) {
	for n := range c {
		fmt.Printf("worker %d received %c\n", id, n)
		//通知外面打印结束
		done <- true
	}
}

func createWorker(id int) worker {
	w := worker{
		in:   make(chan int),
		done: make(chan bool),
	}
	go doWorker(id, w.in, w.done)
	return w
}
